import router from './router'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { Message } from 'element-ui'
import user from '@/store/modules/user'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async(to, from, next) => {
  // 如果进入的页面需要路由验证
  if (to.meta.required) {
    // 判断store中是否有token
    if (user.state.token !== undefined) {
      next()
    } else {
      Message({
        type: 'error',
        message: '请先登录',
        duration: 3000
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

